﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace Bes.Swsp.ToolBox
{
  internal class PythonUtility
  {
    public static Process StartPythonScript(string scriptPath, string arguments = "")
    {
      Process pythonProcess = null;
      string pythonPath = Environment.GetEnvironmentVariable("PYTHONPATH");
      bool resetPythonPath = false;
      try
      {
        string pythonExe = string.Empty;
        if (File.Exists(@"C:\python27\ArcGISx6410.2\python.exe"))
        {
          pythonExe = @"C:\python27\ArcGISx6410.2\python.exe";

          if (pythonPath != null)
          {
            Environment.SetEnvironmentVariable("PYTHONPATH", null);
            resetPythonPath = true;
          }
        }
        else if (File.Exists(@"C:\python27\ArcGIS10.2\python.exe"))
        {
          pythonExe = @"C:\python27\ArcGIS10.2\python.exe";
        }

        if (!string.IsNullOrEmpty(pythonExe))
        {
          pythonProcess = Process.Start(pythonExe, scriptPath.Quoted() + " " + arguments);
        }
        else
        {
          pythonProcess = Process.Start(scriptPath, arguments);
        }
      }
      finally
      {
        if (resetPythonPath)
          Environment.SetEnvironmentVariable("PYTHONPATH", pythonPath);
      }
      return pythonProcess;
    }
  }
}
